package cn.tristenstudy.blog.controller;

import cn.tristenstudy.blog.pojo.Blog;
import cn.tristenstudy.blog.pojo.Type;
import cn.tristenstudy.blog.pojo.dto.TypeDTO;
import cn.tristenstudy.blog.pojo.vo.BlogQuery;
import cn.tristenstudy.blog.service.BlogService;
import cn.tristenstudy.blog.service.TypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 分类展示
 *
 * @author xichen1.li
 * @date 2021-01-13 10:01
 */
@Controller
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    /**
     * 按分类查询博客列表
     *
     * @param id      分类id
     * @param current 页码
     * @param model   model
     * @return 结果
     */
    @GetMapping("/{id}/{current}")
    public String types(@PathVariable("id") Long id, @PathVariable("current") Integer current, Model model) {
        List<TypeDTO> typeList = typeService.listTypeTop(10000);
        if (id == -1) {
            id = typeList.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setCurrent(current);
        blogQuery.setPublished(true);
        blogQuery.setTypeId(id);
        Page<Blog> blogPage = blogService.listBlog(blogQuery);
        model.addAttribute("types", typeList);
        model.addAttribute("page", blogPage);
        model.addAttribute("activeTypeId", id);
        model.addAttribute("first", blogPage.hasPrevious());
        model.addAttribute("last", blogPage.hasNext());
        return "types";
    }

}
